<template>
  <div class="preference-bg">
    <el-card class="preference-card">
      <div class="preference-title">司机偏好设置</div>
      <el-divider />
      <div class="preference-actions">
        <el-button @click="gomy">返回</el-button>
      </div>
      <el-form :model="form" ref="formRef" label-width="100px" class="preference-form">
        <el-form-item label="接单类型" prop="accept_order_type">
          <el-select v-model="form.accept_order_type" multiple placeholder="请选择接单类型">
            <el-option label="快车" value="快车" />
            <el-option label="专车" value="专车" />
            <el-option label="拼车" value="拼车" />
          </el-select>
        </el-form-item>
        <el-form-item label="偏好区域" prop="preferred_area">
          <el-input v-model="form.preferred_area" placeholder="请输入偏好区域" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="large" class="submit-btn" @click="submit">保存</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { http } from '@/utils/request'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
const accept_order_type=ref([])
const router = useRouter()
const formRef = ref()
const form = ref({
  accept_order_type: [],
  preferred_area: '',
  user_id: localStorage.getItem('userid')
})

function fetchPreference() {
  http.get('/login/user_preference/', { "userid": localStorage.getItem('userid') }).then(res => {
    Object.assign(form.value, res.data)
    // 兼容后端返回字符串或数组
    if (Array.isArray(res.data.accept_order_type)) {
      form.value.accept_order_type = res.data.accept_order_type
    } else if (typeof res.data.accept_order_type === 'string') {
      form.value.accept_order_type = res.data.accept_order_type ? res.data.accept_order_type.split(',') : []
    } else {
      form.value.accept_order_type = []
    }
    console.log('回显:', form.value.accept_order_type)
  })
}
function submit() {
  const payload = {
    ...form.value,
    accept_order_type: form.value.accept_order_type.join(',')
  }
  http.post('/login/user_preference/', payload).then(res => {
    ElMessage.success('保存成功')
  })
}
function gomy() {
  router.push('/')
} 
onMounted(fetchPreference)
</script>

<style scoped>
.preference-bg {
  min-height: 100vh;
  background: linear-gradient(135deg, #fff 60%, #ffe5cc 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}
.preference-card {
  width: 480px;
  border-radius: 18px;
  box-shadow: 0 4px 24px 0 rgba(34,34,34,0.08);
  padding: 32px 36px 24px 36px;
}
.preference-title {
  font-size: 26px;
  font-weight: bold;
  color: #222;
  text-align: center;
  margin-bottom: 8px;
  letter-spacing: 2px;
}
.preference-actions {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 12px;
}
.submit-btn {
  width: 100%;
  background: #ff8200;
  border: none;
  font-size: 18px;
  font-weight: bold;
  letter-spacing: 2px;
}
.submit-btn:hover {
  background: #ff9900;
}
</style> 